/**
 * 
 */
package com.qf.apps;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.qf.apps.vo.CourseForm;
import com.qf.domain.Course;
import com.qf.service.CourseService;

/**
 * @author 123
 * 
 */
public class CourseController extends SimpleFormController {
	private CourseService courseService;

	public CourseService getCourseService() {
		return courseService;
	}

	public void setCourseService(CourseService courseService) {
		this.courseService = courseService;
	}

	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		CustomDateEditor dateEditor = new CustomDateEditor(dateFormat, true);
		CustomNumberEditor doubleEditor = new CustomNumberEditor(Double.class,
				true);
		binder.registerCustomEditor(Double.class, doubleEditor);
		binder.registerCustomEditor(Date.class, dateEditor);
		super.initBinder(request, binder);
	}

	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		System.out.println("formBackingObject");
		return new CourseForm();
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		CourseForm courseForm = (CourseForm) command;
		Course course = courseForm.getCourse();
		System.out.println(request.getRequestURI());
		if (course.getName().equals("a")) {
			errors.rejectValue("course.name", "USER_LOGIN_NAME_EXISTED");
			return showForm(request, response, errors);
		}
		if (request.getRequestURI().equals("/tax/manage/addCourse.htm")) {
			Integer id = this.courseService.create(course);
			this.setSuccessView("course/addCourse");
			return super.onSubmit(request, response, command, errors);
		}
		return null;
	}
}
